<?php


namespace App\Services\Auth;


use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;

class PersonalTokenService
{
    static public function make(string $client_id, string $client_secret, string $scope = '*')
    {
        $params = [
            'grant_type'    => 'client_credentials',
            'client_id'     => $client_id,
            'client_secret' => $client_secret,
            'scope'         => $scope,
        ];
        Request()->request->add($params);
        $authRequest = Request::create('/oauth/token', 'POST');
        $authRequest->request->add($params);
        $authRequest->headers->set('Accept', 'application/json');
        $response   = Route::dispatch($authRequest);
        $content    = json_decode($response->getContent(), true);
        $statusCode = $response->getStatusCode();

        return [$statusCode, (200 === $statusCode) ? $content : ($content['message'] ?? '认证失败')];
    }
}
